﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 20.03.2008
 * Zeit: 18:59
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Collections.Generic;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

namespace Gamadus.Builder
{
	/// <summary>
	/// Description of Compiler.
	/// </summary>
	public class Compiler : Gamadus.Core.SingletonManager<Compiler>
	{		
		public void Compile(string file)
		{
			ResourceGenerator runtimeRes = new ResourceGenerator(DirectoryMgr.Instance.TempDir+"runtime.resources");
			runtimeRes.Add(DirectoryMgr.Instance.DataDir+"OpenTK.dll", "OpenTK.dll");
			runtimeRes.Add(DirectoryMgr.Instance.DataDir+"OpenTK.dll.config", "OpenTK.dll.config");
			runtimeRes.Generate();
			CompilerResults cr = CompileGame(file);
			
	        if(cr.Errors.Count > 0)
	        {
	            foreach(CompilerError ce in cr.Errors)
	            {
	            	if(!ce.IsWarning)
	            	{
	            		//throw new BuildException("NATIVE Line "+ce.Line+" "+ce.ErrorNumber+" ("+ce.ErrorText+")");
	            	}
	                Console.WriteLine("  {0}", ce.ToString());
	                Console.WriteLine();
	            }
	        }
//			if(!File.Exists(tempDir+"game_temp.exe"))
//		   	{
//			   	throw new ArgumentException("Couldn't build "+args[0]);
//		   	}
//		   	File.Delete(args[1]);
//		   	File.Copy(tempDir+"game_temp.exe", args[1]);
//			//Mono.Merge.Driver.Execute(new string[] {"-out", args[1], "temp"+dirSep+"game_temp.exe", "data"+dirSep+"SdlDotNet.dll", "data"+dirSep+"Tao.Sdl.dll"});
//			}
//			catch(BuildException e)
//			{
//				Console.WriteLine("ERROR: "+e.Message);
//			}
//			finally
//			{
//				if(args.Length >= 2)
//					Clean();
//			}
		}
		
		CompilerResults CompileGame(string target)
		{
			CSharpCodeProvider provider = new CSharpCodeProvider();
			CompilerParameters cp = new CompilerParameters();
			cp.IncludeDebugInformation = true;
	        // Generate an executable instead of 
	        // a class library.
	        cp.GenerateExecutable = true;
			
	        // Specify the assembly file name to generate.
	        cp.OutputAssembly = target;
	        
	        cp.CompilerOptions = "/define:BUILDERBUILT";
	        
	        //Specify referenced assemblies and embedded resources
	        
	        cp.ReferencedAssemblies.Add("System.Drawing.dll");
	        cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
	        cp.ReferencedAssemblies.Add(DirectoryMgr.Instance.DataDir+"OpenTK.dll");
	        cp.ReferencedAssemblies.Add("System.dll");
	        
	        cp.EmbeddedResources.Add(DirectoryMgr.Instance.TempDir+"runtime.resources");
	        //cp.EmbeddedResources.Add(DirectoryMgr.Instance.TempDir+"includes.resources");
	    
	        cp.GenerateInMemory = false;
	    
	        cp.TreatWarningsAsErrors = false;
	 		List<string> srcFileList = new List<string>();
//	 		foreach(string srcFile in System.IO.Directory.GetFiles(DirectoryMgr.Instance.EngineDir))
//	 		{
//	 			if(srcFile.EndsWith(".cs"))
//	 				srcFileList.Add(srcFile);
//	 		}
	        // Invoke compilation of the source file.
	        return provider.CompileAssemblyFromFile(cp, srcFileList.ToArray());
		}
	}
}
